
var detailData;
var businessTable;
function getBusinessList() {
    if (typeof businessTable !== 'undefined') {
        businessTable.fnPageChange('first');
        return;
    }
    businessTable = $("#businessList").DataTable({
        "sAjaxSource": _path + "/business/queryByPage",     //服务端
        "fnServerParams": function (aoData) {               //ajax请求参数
            aoData.push(
                { "name": "province", "value": $("#province").val() },
                { "name": "classId", "value": $("#classId").val() },
                { "name": "status", "value": $("#status").val() },
                { "name": "fromDate", "value": $("#fromDate").val() },
                { "name": "toDate", "value": $("#toDate").val() },
                { "name": "keyType", "value": $("#keyType").val() },
                { "name": "keywords", "value": $("#keywords").val() }
            );
        },
        "fnServerData": function(sUrl, aoData, fnCallback) {
            var data = new Array();
            for(var i in aoData)
                data.push(aoData[i].name + '=' + aoData[i].value);
            $.ajax( {
                "type": "POST",
                "url": sUrl,
                "dataType": "json",
                "data": data.join('&'),
                "success": function(resp) {
                    detailData = resp.dataList;
                    fnCallback(resp);
                }
            });
        },
        "sServerMethod": "POST",    //请求方式
        "sAjaxDataProp": "dataList",//后台返回数据名
        "aoColumns": [
            { "sTitle": "", "sWidth": "2%",
                "mData": function (obj) {
                    return '<span class="row-details row-details-close" id="'+obj.id+'"></span>';
                }
            },
            { "sTitle": "选择", "sWidth": "6%",
                "mData": function (obj) {
                    return "<input type='checkbox' value='" + obj.id + "'/>";
                }
            },
            { "sTitle": "登记时间", "mData": "createTime", "sWidth": "10%",
                "mRender": function(date) {
                    var date = new Date(date);
                    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                    var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                    var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                    return date.getFullYear() + "-" + month + "-" + currentDate + " " + hh + ":" + mm;
                }
            },
            { "sTitle": "失效日期", "mData": "expiryDate", "sWidth": "10%",
                "mRender": function(date) {
                    if(date) {
                        date = new Date(date);
                        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                        return date.getFullYear() + "-" + month + "-" + currentDate;
                    }
                    return date;
                }
            },
            { "sTitle": "状态", "sWidth": "6%",
                "mData": function (data) {
                    var status = '';
                    switch (data.status) {
                        case '0':
                            status = '<font color="red">未审核</font>';
                            break;
                        case '1':
                            status = (data.isRecommend === '1' ? '<font color="red">推荐</font>' : '上架');
                            break;
                        case '2':
                            status = '下架';
                            break;
                        case '3':
                            status = '已删除';
                            break;
                        default:
                            status = '<font color="gray">未知</font>';
                            break;
                    }
                    return status;
                }},
            { "sTitle": "地区", "mData": "province", "sWidth": "7%" },
            { "sTitle": "分类", "mData": "bClass.value0", "sWidth": "7%" },
//            { "sTitle": "开业时间", "mData": "opeanDate", "sWidth": "8%" },
            { "sTitle": "标题", "mData": "summary", "sWidth": "8%" },
            { "sTitle": "姓名", "mData": "userName", "sWidth": "8%" },
            { "sTitle": "电话", "mData": "telephone", "sWidth": "8%" },
            { "sTitle": "面积", "mData": "acreage", "sWidth": "6%" },
            { "sTitle": "资金", "mData": "inmoney", "sWidth": "6%" },
            /*{ "sTitle": "上传者", "mData": "member", "sWidth": "10%",
                "mRender": function(obj) {
                    return obj==null?'游客':obj.userName;
                }
            },*/
            { "sTitle": "评论", "sWidth": "10%",
                "mData": function (data) {
                    var htmlContent = '';
                    if (data.discuss && data.discuss.length>0) {
                        htmlContent = '<a href="javascript:modalPL(\'' + data.id + '\')" class="btn default btn-xs black"><i class="fa fa-comments-o" title="查看评论"></i></a>'
                    } else {
                        htmlContent = "<td>无</td>";
                    }
                    return htmlContent;
                }
            },
            { "sTitle": "操作", "sWidth": "10%",
                "mData": function (data) {
                    var htmlContent = '<a href="' + _path + '/business/edit/' + data.id + '" class="btn default btn-xs purple"><i class="fa fa-edit" title="编辑"></i></a>';
                    var creater = data.creater;
                    if (typeof creater === 'string' && creater.length > 0)
                        htmlContent += '<a href="javascript:modalXX(\'' + creater + '\',\'' + data.id + '\')" class="btn default btn-xs green"><i class="fa fa-envelope-o" title="发送消息"></i></a>'
                    return htmlContent;
                }
            }
        ]
    });
    $('#businessList').on('click', ' tbody td .row-details', function () {
        var nTr = $(this).parents('tr')[0];
        if ( businessTable.fnIsOpen(nTr) ) {
            /* This row is already open - close it */
            $(this).addClass("row-details-close").removeClass("row-details-open");
            $(this).parent("td").next().children("input").click();
            businessTable.fnClose( nTr );
        }else {
            /* Open this row */
            $(this).addClass("row-details-open").removeClass("row-details-close");
            $(this).parent("td").next().children("input").click();
            businessTable.fnOpen( nTr, fnFormatDetails(nTr), 'details' );
        }
    });
}

function oper(type) {
    var inputs = $("input:checked");
    if (inputs.length > 0) {
        var id = new Array();
        for (var i = 0, len = inputs.length; i < len; i++) {
            id.push(inputs[i].value);
        }
        $.ajax({
            url: _path + "/business/updBatch/"+type,
            type: "POST",
            data: {"id" : id},
            async: false,
            success: function (data) {
                if(data.statusCode === 200) {
                    if(type === 'S') {
                        modalMoney(id, '${data.username}');
                    }else {
                        alert("操作成功！");
                        window.location.reload();
                    }
                } else if (data.statusCode === 500) {
                    alert("操作失败！");
                }
            }
        });
    } else {
        alert("请选择要修改的记录！");
    }
}

function modifiy(uri) {
    $('#panelDetail').on('shown.bs.modal', function () {
        $("#detail", $("#panelDetail")).attr("src", uri);
    }).modal('show');
}
function modalXX(creater, businessId) {
    $('#panel').on('shown.bs.modal', function () {
        $("#creater", $("#panel")).val(creater);
        $("#businessId", $("#panel")).val(businessId);
//        $("#xxtype", $("#panel")).val(3);// 信息类型 我的贡献对应的商机
    }).modal('show');
}
function modalPL(businessid) {
    $(".timeline").html('');
    this.businessid = businessid;
    $('#panelPL').modal('show');
}
function modalMoney(id, name) {
    $('#panelMoney').on('shown.bs.modal', function () {
        $("#businessid", $("#panelMoney")).val(id);
        $("#money", $("#panelMoney")).val('');
    }).modal('show');
}
function htmlPLContent(data) {
    var html = new Array();
    if (data.statusCode === 200 && data.dataList.length > 0) {
        var dataList = data.dataList;
        for (var i = 0, len = dataList.length; i < len; i++) {
            data = dataList[i];
            var date = data.createTime.split(" ");
            html.push('<li class="timeline-' + rd() + '">');
            html.push('<div class="timeline-time">');
            html.push('<span class="date">');
            html.push(date[0]);
            html.push('</span>');
            html.push('<span class="time">');
            html.push(date[1]?date[1].substr(0,5):'');
            html.push('</span>');
            html.push('</div>');
            html.push('<div class="timeline-icon">');
            html.push('<img class="imgRound" src="'+ _path + data.userPhoto + '" />');
            html.push('</div>');
            html.push('<div class="timeline-body">');
            html.push(data.username);
            html.push('<div class="timeline-content">');
            html.push(data.context);
            html.push('</div></div></li>');
        }
    } else {
        html.push('<li class="timeline-' + rd() + '">');
        html.push('<div class="timeline-time">');
        html.push('<span class="date">');
        html.push('--/--/----');
        html.push('</span>');
        html.push('<span class="time">');
        html.push('--:--');
        html.push('</span>');
        html.push('</div>');
        html.push('<div class="timeline-icon">');
        html.push('<img class="imgRound" src="${path}/' + data.userphoto + '" />');
        html.push('</div>');
        html.push('<div class="timeline-body">');
        html.push('游客');
        html.push('<div class="timeline-content">');
        html.push('暂无评论');
        html.push('</div></div></li>');
    }
    $(".timeline").html(html.join(''));
}
function rd() {
    var cs = "";
    switch (Math.floor(Math.random() * 5 + 1)) {
        case 1:
            cs = "yellow";
            break;
        case 2:
            cs = "blue";
            break;
        case 3:
            cs = "green";
            break;
        case 4:
            cs = "red";
            break;
        case 5:
            cs = "purple";
            break;
        default:
            cs = "yellow";
            break;
    }
    return cs;
}

function fnFormatDetails(nTr) {
    var html = new Array();
    var data = detailData[nTr._DT_RowIndex]
    html.push('<form id="subTable">');
    html.push('<table width="100%">');
    html.push('<tr>');
    html.push('<td>标题:</td>');
    html.push('<td>' + conver(data.summary) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>姓名:</td>');
    html.push('<td>' + conver(data.username) + '</td>');
    html.push('<td>联系电话:</td>');
    html.push('<td>' + conver(data.telephone) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>代理地区:</td>');
    html.push('<td>' + conver(data.province) + conver(data.city) + conver(data.county) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>乡/镇:</td>');
    html.push('<td>' + conver(data.address) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>面积:</td>');
    html.push('<td>' + conver(data.acreage) + '</td>');
    html.push('<td>投入金额:</td>');
    html.push('<td>' + conver(data.inmoney) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>分类:</td>');
    html.push('<td>' + conver(data.classname) + '</td>');
    html.push('<td>品类:</td>');
    html.push('<td>' + conver(data.pinleiname) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>代理档次:</td>');
    html.push('<td>' + conver(data.agentsGrade) + '</td>');
    html.push('<td>星数/价格:</td>');
    html.push('<td>' + conver(data.price) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>库存:</td>');
    html.push('<td>' + conver(data.stock) + '</td>');
    html.push('<td>开业时间:</td>');
    html.push('<td>' + conver(data.opeanDate) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>详细信息:</td>');
    html.push('<td>' + conver(data.agentDetails) + '</td>');
    html.push('</tr>');
    html.push('</table>');
    html.push('</form>');
    return html.join('');
}